<?php
/* this file will edit basket item */
// load wordpress defaults, make global variables/functions available 
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' ); 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

<!-- head start -->

<head profile="http://gmpg.org/xfn/11">
	<?php
    $aOptions = SofaSuppaStore::initOptions( false );
	$entrycurrency = $aOptions[ 'entrycurrency' ];
	$num_of_mb = $aOptions[ 'num_of_def_boxes' ];
	$use_stock = $aOptions[ 'use_stock' ];
	if( $num_of_mb <= 0 ) $num_of_mb = 1;
	// handle font selection
	$google_fonts = $aOptions[ 'google_fonts' ];
	$font_str = $google_fonts;
	$font_str_arr = explode( ':', $font_str );
	$font_name = $font_str_arr[ 0 ];
	$clean_font_arr = explode( '+', $font_name );
	$clean_font_name = implode( ' ', $clean_font_arr );
    ?>
    <link href='http://fonts.googleapis.com/css?family=<?php echo $google_fonts; ?>' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/960.css' ); ?>" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/screen.css' ); ?>" type="text/css" media="screen, projection" />
    <style type="text/css">
	/* handle basic styles according to google fonts selection */
	h1, h2, h3, h4, h5, h6 { font-family: '<?php echo $clean_font_name; ?>', Arial, sans-serif; font-weight: normal; }
	</style>
	<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen, projection" />
    
    <?php wp_print_scripts( 'jquery' ); ?>
    <script src="<?php echo( get_template_directory_uri() . '/script/jquery.suppastore.custom.js' ); ?>" type="text/javascript"></script>
</head>

<!-- head end -->

<body>

<!-- main content start -->

<div id="lightcontent" class="container_8 clearfix">
    
	<?php
	// get item details
	if( $_SERVER[ 'REQUEST_METHOD' ] == 'GET' ) {
		$itemid    = !empty( $_GET[ 'itemid' ] ) ? intval( urldecode( $_GET[ 'itemid' ] ) ) : NULL; // product id
		$itemsize  = !empty( $_GET[ 'itemsize' ] ) ? stripslashes( strip_tags( urldecode( $_GET[ 'itemsize' ] ) ) ) : NULL; // product size
		$cookid    = !empty( $_GET[ 'cookid' ] ) ? stripslashes( strip_tags( urldecode( $_GET[ 'cookid' ] ) ) ) : NULL; // cookie id
		$itemqty   = !empty( $_GET[ 'itemqty' ] ) ? intval( urldecode( $_GET[ 'itemqty' ] ) ) : NULL; // quantity
		$itemstock = !empty( $_GET[ 'itemstock' ] ) ? intval( urldecode( $_GET[ 'itemstock' ] ) ) : NULL; // in stock
		$itemsel   = !empty( $_GET[ 'itemsel' ] ) ? stripslashes( strip_tags( urldecode( $_GET[ 'itemsel' ] ) ) ) : NULL; // selected row
		$itemprice = !empty( $_GET[ 'itemprice' ] ) ? floatval( urldecode( $_GET[ 'itemprice' ] ) ) : NULL; // price
		$itemshipp = !empty( $_GET[ 'itemshipp' ] ) ? floatval( urldecode( $_GET[ 'itemshipp' ] ) ) : NULL; // individual shipping price
	} elseif( $_SERVER[ 'REQUEST_METHOD' ] == 'REQUEST' ) {
		$itemid    = !empty( $_REQUEST[ 'itemid' ] ) ? intval( urldecode( $_REQUEST[ 'itemid' ] ) ) : NULL; // product id
		$itemsize  = !empty( $_REQUEST[ 'itemsize' ] ) ? stripslashes( strip_tags( urldecode( $_REQUEST[ 'itemsize' ] ) ) ) : NULL; // product size
		$cookid    = !empty( $_REQUEST[ 'cookid' ] ) ? stripslashes( strip_tags( urldecode( $_REQUEST[ 'cookid' ] ) ) ) : NULL; // cookie id
		$itemqty   = !empty( $_REQUEST[ 'itemqty' ] ) ? intval( urldecode( $_REQUEST[ 'itemqty' ] ) ) : NULL; // quantity
		$itemstock = !empty( $_REQUEST[ 'itemstock' ] ) ? intval( urldecode( $_REQUEST[ 'itemstock' ] ) ) : NULL; // in stock
		$itemsel   = !empty( $_REQUEST[ 'itemsel' ] ) ? stripslashes( strip_tags( urldecode( $_REQUEST[ 'itemsel' ] ) ) ) : NULL; // selected row
		$itemprice = !empty( $_REQUEST[ 'itemprice' ] ) ? floatval( urldecode( $_REQUEST[ 'itemprice' ] ) ) : NULL; // price
		$itemshipp = !empty( $_REQUEST[ 'itemshipp' ] ) ? floatval( urldecode( $_REQUEST[ 'itemshipp' ] ) ) : NULL; // individual shipping price
	}
	// get the post
	$post_obj = get_post( $itemid );
	?>
    
    <?php
	// meta stuff
	$arr_price = array();
	$arr_sizes = array();
	$arr_stock = array();
	$arr_shipp = array();
	$iter_meta = 0;
	while( $iter_meta < $num_of_mb ) {
		$single_price = 'sofa_' . $iter_meta . '_textarea_price';
		$single_sizes = 'sofa_' . $iter_meta . '_textarea_size';
		$single_stock = 'sofa_' . $iter_meta . '_textarea_stock';
		$single_shipp = 'sofa_' . $iter_meta . '_textarea_shipp';
		// populate array
		$single_id = $post_obj->ID; // just to speed it up
		if( get_post_meta( $single_id, $single_price, true ) != '*' ) {
			array_push( $arr_price, get_post_meta( $single_id, $single_price, true ) );
			array_push( $arr_sizes, get_post_meta( $single_id, $single_sizes, true ) );
			if( $use_stock == 'yes' ) {
				$stock_post_meta_value = get_post_meta( $single_id, $single_stock, true );
				if( $stock_post_meta_value == '*' ) array_push( $arr_stock, 0 );
				else array_push( $arr_stock, $stock_post_meta_value );
			}
			if( get_post_meta( $single_id, $single_shipp, true ) != '*' ) array_push( $arr_shipp, get_post_meta( $single_id, $single_shipp, true ) );
		}
		 
		$iter_meta ++;
	}
	?>

    <div id="featured" class="castshadow grid_8">
    
    	<!-- title start -->
        
        <h2 class="grid_8 alpha omega"><?php _e( "EDIT", "sofa_suppastore" ); ?> <?php echo get_the_title( $post_obj->ID ); ?></h2>
        
        <!-- title end -->
        
        <!-- product image start -->
        
        <div id="prodimageset" class="grid_4 alpha">
        
        	<?php
			// any featured image?
			if( get_the_post_thumbnail( $post_obj->ID, 'featured' ) ) {
				echo '<div class="grid_4 alpha omega">';
				$img_attr = array( 'class'	=> 'attachment-featured alignleft' );
				echo get_the_post_thumbnail( $post_obj->ID, 'featured', $img_attr );
				echo '</div>';
			} else {
				echo '<div class="grid_4 alpha omega">';
				$img_attr = array( 'class'	=> 'size-medium alignleft' );
				echo get_the_post_thumbnail( $post_obj->ID, 'medium', $img_attr );
				echo '</div>';
			}
			?>
        
        </div>
        
        <!-- product image end -->
        
        <!-- product in basket details start -->
        
        <div id="proddetails" class="grid_4 omega">
        
        <h2 id="prodprice"><?php echo $entrycurrency . ' ' . number_format( $arr_price[ 0 ], 2, '.', '' ); ?></h2>
        
        	<table id="tblmeta" width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <th scope="col" class="textleft"><?php _e( "Size", "sofa_suppastore" ); ?></th>
                <th scope="col" class="<?php if( empty( $arr_shipp ) && $use_stock == 'no' ) echo 'textright'; else echo 'center'; ?>"><?php echo __( "Price", "sofa_suppastore" ) . ' ' . '(' . $entrycurrency . ')'; ?></th>
                <?php if( !empty( $arr_shipp ) ) { ?><th scope="col" class="<?php if( $use_stock == 'no' ) echo 'textright'; else echo 'center'; ?>"><?php echo __( "Shipping", "sofa_suppastore" ) . ' ' . '(' . $entrycurrency . ')'; ?></th><?php } ?>
                <?php if( $use_stock == 'yes' ) { ?><th scope="col" class="textright"><?php _e( "Stock", "sofa_suppastore" ); ?></th><?php } ?>
              </tr>
              
              <?php
			  	// loop through meta
				$iter_chck = 0;
				$iter_loop = 0;
				$iter_leng = count( $arr_price );
				while( $iter_loop < $iter_leng ) {
					if( $use_stock == 'yes' ) {
						if( $arr_stock[ $iter_loop ] != '*' && $arr_stock[ $iter_loop ] > 0 ) { // don't list products that are out of stock!
							$iter_chck ++;
							echo '<tr id="infoline_' . $iter_loop . '" class="proddetailstr">';
							echo '<td title="size" class="optionstill textleft">' . $arr_sizes[ $iter_loop ] . '</td>';
							echo '<td title="price" class="center">' . number_format( $arr_price[ $iter_loop ], 2, '.', '' ) . '</td>';
							if( !empty( $arr_shipp ) ) echo '<td title="shipp" class="center">' . number_format( $arr_shipp[ $iter_loop ], 2, '.', '' ) . '</td>';

							$true_stock_value = 0; 
							if( $arr_stock[ $iter_loop ] != '*' ) $true_stock_value = $arr_stock[ $iter_loop ];
							echo '<td title="stock" class="textright">' . $true_stock_value . '</td>'; 
							
							echo '</tr>';
						}
					} elseif( $use_stock == 'no' ) {
						$iter_chck ++;
						echo '<tr id="infoline_' . $iter_loop . '" class="proddetailstr">';
						echo '<td title="size" class="optionstill textleft">' . $arr_sizes[ $iter_loop ] . '</td>';
						if( !empty( $arr_shipp ) ) echo '<td title="price" class="center">' . number_format( $arr_price[ $iter_loop ], 2, '.', '' ) . '</td>';
						else echo '<td title="price" class="textright">' . number_format( $arr_price[ $iter_loop ], 2, '.', '' ) . '</td>';
						if( !empty( $arr_shipp ) ) echo '<td title="shipp" class="textright">' . number_format( $arr_shipp[ $iter_loop ], 2, '.', '' ) . '</td>';
						echo '</tr>';
					}
					
					$iter_loop ++;
					
				}
				if( $iter_chck == 0 ) {
					echo '<tr>';
					echo '<td colspan="4" class="textleft">' . __( "Product not available!", "sofa_suppastore" ) . '</td>';
					echo '</tr>';
				}
			  ?>
              
            </table>
            
            <form id="buyme" method="post" action="<?php echo get_template_directory_uri() . '/cart_handler.php'; ?>">
            
            	<?php if( $iter_chck > 0 ) { ?>
            
                <div class="grid_4 alpha omega">
                    
                    <div class="left"><span class="qty"><?php _e( "Quantity", "sofa_suppastore" ); ?> </span><input type="text" name="itemqty" id="itemqty" value="1" /></div>
                    <div class="right"><button class="add-to-cart" type="submit" tabindex="2"><?php _e( "Save to basket", "sofa_suppastore" ); ?></button></div>
                    
                </div>
                
                <?php } ?>
                
                <input type="hidden" name="itemselmeta" id="itemselmeta" value="" />
                <input type="hidden" name="itemid" id="itemid" value="<?php echo $post_obj->ID; ?>" />
                <input type="hidden" name="dothis" id="dothis" value="editme" />
                <input type="hidden" name="itemsize" id="itemsize" value="" />
                <input type="hidden" name="itemprice" id="itemprice" value="" />
                <input type="hidden" name="itemstock" id="itemstock" value="" />
                <input type="hidden" name="itemshipp" id="itemshipp" value="" />
                <input type="hidden" name="cookid" id="cookid" value="<?php echo $cookid; ?>" />
                <?php wp_nonce_field( 'chck_ref_cc', 'sofa_suppastore_cc' ); ?>
            
            </form>
        
        </div>
        
        <!-- product in basket details end -->
        
        <script type="text/javascript">
        
        jQuery( document ).ready( function() {
            
            // table tr hover
            jQuery( function() {
                jQuery( 'table .proddetailstr' ).hover( function() {
                    jQuery( this ).addClass( 'hovered' );
                    jQuery( this ).css( 'cursor', 'default' );
                }, 
                function() {
                    jQuery( this ).removeClass( 'hovered' );
                } )
            } );
            
            // populate fields
			jQuery( '#itemselmeta' ).val( "<?php echo $itemsel; ?>" );
			jQuery( '#itemsize' ).val( "<?php echo $itemsize; ?>" );
			jQuery( '#itemprice' ).val( "<?php echo $itemprice; ?>" );
			<?php if( $use_stock == 'yes' ) { ?>jQuery( '#itemstock' ).val( "<?php echo $itemstock; ?>" );<?php } ?>
			jQuery( '#itemshipp' ).val( "<?php echo $itemshipp; ?>" );
			jQuery( '#itemqty' ).val( "<?php echo $itemqty; ?>" );
			
            var written_meta  = jQuery( '#itemselmeta' ).val();
            var written_price = jQuery( '#itemprice' ).val();
            if( written_meta != '' ) jQuery( written_meta + ' td:first-child' ).removeClass( 'optionstill' ).addClass( 'optionactiv' );
            else jQuery( '#infoline_0 td:first-child' ).removeClass( 'optionstill' ).addClass( 'optionactiv' );
			
            if( written_price != '' ) jQuery( '#prodprice' ).html( "<?php echo $entrycurrency . ' '; ?>" + number_format( written_price, 2, '.', '' ) );
            
            // handle selections
            jQuery( '.proddetailstr' ).click( function( event ) {
                var this_id = '#' + jQuery( this ).attr( 'id' );
                var options_length = jQuery( this_id + ' td' ).length;
                if( options_length > 0 ) {
                    var ii = 0;
                    while( ii < options_length ) { // faster than jQuery.each
                        var cell_value = jQuery( this_id + ' td' ).eq( ii ).text();
                        var cell_title = jQuery( this_id + ' td' ).eq( ii ).attr( 'title' );
                        switch( cell_title ) {
                            case 'size': jQuery( '#itemsize' ).val( encodeURIComponent( cell_value ) );
                            break;
                            case 'price': jQuery( '#itemprice' ).val( encodeURIComponent( cell_value ) );
                            break;
							<?php if( $use_stock == 'yes' ) { ?>
                            case 'stock': jQuery( '#itemstock' ).val( encodeURIComponent( cell_value ) );
                            break;
							<?php } ?>
                            case 'shipp': jQuery( '#itemshipp' ).val( encodeURIComponent( cell_value ) );
                            break;
                            default: 0;
                        }
                        ii ++;
                    }
                    
                    // remember selection
                    if( jQuery( this ).attr( 'id' ).substring( 0, 8 ) == 'infoline' ) jQuery( '#itemselmeta' ).val( this_id );
                    
                    // remove all active selections
                    var my_parent = '#' + jQuery( this_id ).parent().parent().attr( 'id' );
                    jQuery( my_parent + ' tr td:first-child' ).each( function() {
                        jQuery( this ).removeClass( 'optionactiv' ).addClass( 'optionstill' );
                    } );
                    
                    // switch class to selected
                    jQuery( this_id + ' td:first-child' ).removeClass( 'optionstill' ).addClass( 'optionactiv' );
                    
                    // update main price
                    jQuery( '#prodprice' ).html( "<?php echo $entrycurrency . ' '; ?>" + jQuery( '#itemprice' ).val() );
                }
            } );
        } );
		
		// format number
		function number_format( number, decimals, dec_point, thousands_sep ) {
			number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
			var n = !isFinite(+number) ? 0 : +number,
			prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
			sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
			dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
			s = '',
			toFixedFix = function (n, prec) {
				var k = Math.pow(10, prec);
				return '' + Math.round(n * k) / k;
			};
			// Fix for IE parseFloat(0.55).toFixed(0) = 0;
			s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
			if (s[0].length > 3) {
				s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
			}
			if ((s[1] || '').length < prec) {
				s[1] = s[1] || '';
				s[1] += new Array(prec - s[1].length + 1).join('0');
			}
			return s.join(dec);
		}
        
        </script>
        
    </div>
        
</div>

<!-- main content end -->

</body>
</html>